var d;
var demo;
$(function() {
    //开启全屏
    fullDialogModal();
    //开启验证
    demo = $("#editForm").Validform({
        tiptype: 3
    });
    if ($("#can_save").val() == '1') {
        addDialogButton("保存", save);
    }
    if ($("#can_audit").val() == '1') {
        addDialogButton("保存并提交", submit);
    }
    if ($("#can_pass").val() == '1') {
        addDialogButton("通过", pass, 'audit', '#299c48');
    }
    if ($("#can_refused").val() == '1') {
        addDialogButton("拒绝", refused, 'audit', '#FFA500');
    }
    $("#receive_time").change(function(){
		var value = $(this).val();
		var dateYear = value.split('-')[0];
		$("#award_year").val(dateYear);
	})
    
    laydate.render({
        elem: '#receive_time',
        trigger: 'click',
        done: function(value, date) {
            $('#receive_time').val(value);
            var dateYear = value.split('-')[0];
            $("#award_year").val(dateYear);
        }
    });

    laydate.render({
        elem: '#birthday_t',
        trigger: 'click',
        done: function(value, date) {
            $('#birthday_t').val(value);
        }
    });
    laydate.render({
        elem: '#birthday_s',
        trigger: 'click',
        done: function(value, date) {
            $('#birthday_s').val(value);
        }
    });
    laydate.render({
		elem: '#award_year',
		trigger: 'click',
		type: 'year',
		done: function (value, date) {
			$('#award_year').val(value);
		}
	});
    // 新增
    $('#upload_img').change(function() {
        uploadImg();
    });
    $('#filename').change(function() {
        uploadFile(this);
    });
});
var save = function() {
    $("#save_type").val(1);
    var examine_level = $("#examine_level").find('option:selected').val();
    var is_submit_cg = $("#is_submit_cg").val();
    if((examine_level == '校级' || examine_level == '分院级') && is_submit_cg == 1){
        swal({
            title: "请确认",
            text: '是否确认考核等级为校级或分院级的内容提交到成果?',
            type: "warning",
            showCancelButton: true,
            closeOnConfirm: true,
            confirmButtonText: "确定",
            cancelButtonText: '取消',
            confirmButtonColor: "#ec6c62"
        }, function(isConfirm) {
            if (isConfirm) {
                saveButton();
            }
        });
    }else{
        saveButton();
    }
}
var submit = function() {
    $("#save_type").val(2);
    saveButton();
}

function saveButton() {
    if (!demo.check()) {
        $('.Validform_error:first').focus();
        return;
    }
    if($('#journal_level').find('option:selected').val() == -1){
        window.parent.topMessagePrompt('error', "请选择刊物级别");
        return;
    } else if($('#subject').find('option:selected').val() == -1){
        window.parent.topMessagePrompt('error', "请选择学科门类");
        return;
    } else if($('#type').find('option:selected').val() == -1){
        window.parent.topMessagePrompt('error', "请选择论文类型");
        return;
    } else if($('#dept_name').find('option:selected').val() == -1){
        window.parent.topMessagePrompt('error', "请选择所属单位");
        return;
    } else if (($('.showFileName').length <= 0) && ($('.document-img').length <= 1)) {
        window.parent.topMessagePrompt('error', "请上传图片或附件");
        return;
    }
    var memberList = $('.member-tr .member-tr-in');
    var is_complete = true;
    // $.each(memberList, function(k, v) {
    //     var identity = $(v).find('.identity').val();
    //     if (identity != -1) {
    //         // 判断数组中是否有未选的，未存在的，没有才能保存
    //         //已选择身份 判断这个人是否存在
    //         // var author = $(v).find(".author").val();
    //         if(identity == 3){
    //              var author = $(v).find(".author_name").val();
    //         }else{
    //              var author = $(v).find(".author").val();
    //         }
    //         var is_author = isAuthorCheck(identity, author);
    //         if (is_author == false) {
    //             is_complete = false;
    //             // 不存在
    //             swal({
    //                 title: "提示",
    //                 text: "第" + parseInt(k + 1) + "行该人员不存在，请添加信息",
    //                 type: "warning",
    //                 showCancelButton: false,
    //                 closeOnConfirm: true,
    //                 confirmButtonText: "确定",
    //                 confirmButtonColor: "#ec6c62"
    //             }, function(isConfirm) {
    //                 if (isConfirm) {
    //                     $(v).find(".author").val('');
    //                     $('.edit-mask').show();
    //                     if (identity == 1) {
    //                         $('.teacher-msg .save-btn1')[0].setAttribute('data-index',k);
    //                         $('.teacher-msg').show();
    //                         $('.student-msg').hide();
    //                         $('.outside-teacher-msg').hide();
    //                     } else if (identity == 2) {
    //                         $('.student-msg .save-btn2')[0].setAttribute('data-index',k);
    //                         $('.teacher-msg').hide();
    //                         $('.student-msg').show();
    //                         $('.outside-teacher-msg').hide();
    //                     } else if (identity == '3') {
    //                         $('.outside-teacher-msg .save-btn3')[0].setAttribute('data-index', k);
    //                         $('.teacher-msg').hide();
    //                         $('.student-msg').hide();
    //                         $('.outside-teacher-msg').show();
    //                     }
    //                 }
    //             });
    //         }
    //     } else {
    //         swal({
    //             title: "提示",
    //             text: "请先选择第" + parseInt(k + 1) + "行身份",
    //             type: "warning",
    //             showCancelButton: false,
    //             closeOnConfirm: true,
    //             confirmButtonText: "确定",
    //             confirmButtonColor: "#ec6c62"
    //         })
    //         return;
    //     }
    // })
    if (is_complete == true) {
        // 存在这个人才能保存
        d = dialog({
            title: '',
            content: "数据保存中,请稍后...",
        });
        d.showModal();
        var datas = getFormJson();
        // console.log(datas);
        $.ajax({
            url: "Admin/Subjectplatform/save",
            type: "post",
            data: datas,
            success: function(data) {
                if (d != undefined) d.close().remove();
                var ret = $.parseJSON(data);
                if (ret.status) {
                    var v = $.parseJSON(ret.v);
                    if (v == 1) {
                        window.parent.refreshTable();
                        showDialogModalConfirm('success', '保存成功！', ret.msg, '继续添加', '返回列表', function() {
                            $("form")[0].reset();
                        }, function() {
                            hideDialogModal();
                        });
                    } else {
                        window.parent.topMessagePrompt('success', ret.msg);
                        window.parent.refreshTable();
                        hideDialogModal();
                    }
                } else {
                    window.parent.topMessagePrompt('error', ret.msg);
                }
            }
        });
    }
}
// 判断是否存在
function isAuthorCheck(identity, author) {
    is_author = true;
    $.ajax({
        type: "POST",
        url: getRootPath() + "/Admin/User/is_author",
        data: {
            identity: identity,
            author: author
        },
        dataType: "json",
        async: false,
        success: function(data) {
            if (data.v != true) {
                is_author = false
            }
        },
        error: function(data) {
            console.log('isAuthor error', data)
        }
    });
    //全局判断
    if (is_author) {
        return true;
    } else {
        return false;
    }
}

function addStudent() {
    let demo = $('.member-demo').html();
	$('.member-tr td').append(demo);
}

function delStudent(obj) {
	$(obj).parents('.member-tr-in').remove();
}


// 新增
// 触发图片上传
function upload(obj) {
    $('#upload_img').click();
}

function delImg(obj) {
    $(obj).parent().remove();
}

function delFile(obj) {
    $(obj).parent().remove();
}

function bannerPrev(obj) {
    var can_prev = $(obj).parents('.data-box').prev().hasClass('data-box-banner');
    if (can_prev) {
        $(obj).parents('.data-box').prev().before($(obj).parents('.data-box'));
    }
}

function bannerNext(obj) {
    var can_next = $(obj).parents('.data-box').next().hasClass('data-box-banner');
    if (can_next) {
        $(obj).parents('.data-box').next().after($(obj).parents('.data-box'));
    }
}
// 图片上传
function uploadImg() {
    var fileObj = document.getElementById("upload_img").files; // js 获取文件对象
    if (fileObj.length == 0) {
        errorAlert('请选择图片');
        return;
    }
    var formFile = new FormData();
    for (var i = 0; i < fileObj.length; i++) {
        formFile.append("myfile", fileObj[i]); //加入文件对象
    }
    var data = formFile;
    $.ajax({
        url: 'Admin/CommonUpload/uploadImg',
        data: data,
        type: "POST",
        dataType: "json",
        cache: false, //上传文件无需缓存
        processData: false, //用于对data参数进行序列化处理 这里必须false
        contentType: false, //必须       
        success: function(data) {
            if (data.status) {
                // alert(now_type);
                $('.data-box-demo img').attr('src', getRootPath() + '/' + data.v);
                $('.data-box-demo a').attr('href', getRootPath() + '/' + data.v);
                $('.data-box-demo .path').val(getRootPath() + '/' + data.v).attr('name', 'img[]');
                $('.img-box').append($('.data-box-demo').html());
            } else {
                window.parent.topMessagePrompt('error', data.msg);
            }
        }
    })
}
// 触发文件上传
function upload1() {
    $('#filename').click();
}

function uploadFile(obj) {
    var filePath = $(obj).val();
    doFile(obj); //上传图片
}


// 审核通过
function pass() {
    swal({
        title: "请确认",
        text: '是否确认通过审核',
        type: "warning",
        showCancelButton: true,
        closeOnConfirm: true,
        confirmButtonText: "确定",
        cancelButtonText: '取消',
        confirmButtonColor: "#ec6c62"
    }, function(isConfirm) {
        if (isConfirm) {
            $.post(getRootPath() + '/Admin/Audit/pass', {
                type: $('#type').val(),
                audit_id: $('#id').val()
            }, function(data) {
                if (data.status) {
                    window.parent.topMessagePrompt('success', data.msg);
                    window.parent.refreshTable();
                    hideDialogModal();
                } else {
                    window.parent.topMessagePrompt('error', data.msg);
                }
            }, 'json');
        }
    });
}
// 审核拒绝
function refused() {
    showDialogModalConfirm('', '请输入拒绝原因', '<p>原因:</p><textarea id="refused_remark" rows="5" style="width:100%"></textarea>', '确认拒绝', '取消', function() {
        var remark = $("#refused_remark").val();
         if(remark == ""){
                window.parent.topMessagePrompt('error', "请输入拒绝原因");
                return;
            }else{
                 $.post(getRootPath() + '/Admin/Audit/refused', {
                    type: $('#type').val(),
                    audit_id: $('#id').val(),
                    remark: remark
                }, function(data) {
                    if (data.status) {
                        window.parent.topMessagePrompt('success', data.msg);
                        window.parent.refreshTable();
                        hideDialogModal();
                    } else {
                        window.parent.topMessagePrompt('error', data.msg);
                    }
                }, 'json');
            }
    }, function() {
        hideDialogModal();
    })
}
